package com.jungle.mvvm.sample.ui.collect;

import android.annotation.SuppressLint;
import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;

import com.jungle.mvvm.base.BaseViewModel;
import com.jungle.mvvm.binding.command.BindingAction;
import com.jungle.mvvm.binding.command.BindingCommand;
import com.jungle.mvvm.sample.BR;
import com.jungle.mvvm.sample.R;
import com.jungle.mvvm.sample.app.WanObserver;
import com.jungle.mvvm.sample.bean.CollectBean;
import com.jungle.mvvm.sample.bean.ListBean;
import com.jungle.mvvm.sample.http.WanNetwork;
import com.jungle.mvvm.sample.ui.toolbar.ToolBarViewModel;
import com.jungle.mvvm.utils.KLog;
import com.jungle.mvvm.utils.ToastUtils;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * @ClassName CollectViewModel
 * @Description CollectViewModel
 * @Author Daniel
 * @Version V1.0.0
 */
public class CollectViewModel extends BaseViewModel {
    private boolean mRefresh = true;
    private boolean mOver = false;
    private int mSize = 0;
    public ToolBarViewModel toolBarViewModel;

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public CollectViewModel(@NonNull Application application) {
        super(application);
        toolBarViewModel = new ToolBarViewModel(this);
        toolBarViewModel.title.set("我的收藏");
    }

    //给RecyclerView添加ObservableList
    public ObservableList<CollectItemViewModel> observableList = new ObservableArrayList<>();
    //RecyclerView多布局添加ItemBinding
    public ItemBinding<CollectItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_collect);

    public class UIChangeObservable {
        //下拉刷新完成
        public ObservableBoolean finishRefreshing = new ObservableBoolean(false);
        //上拉加载完成
        public ObservableBoolean finishLoadmore = new ObservableBoolean(false);
    }

    //下拉刷新
    public BindingCommand onRefreshCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            mRefresh = true;
            mSize = 0;
            getData();
        }
    });

    //上拉加载
    public BindingCommand onLoadMoreCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            mRefresh = false;
            if (mOver) {
                ToastUtils.showShort("没有更多数据了！");
                changeState();
            } else {
                getData();
            }
        }
    });

    public void initData() {
        this.mSize = 0;
        this.mRefresh = true;
        getData();
    }

    @SuppressLint("CheckResult")
    private void getData() {
        showDialog("数据加载中……");
        CollectModel.getCollectionList(mSize)
                .compose(WanNetwork.applyService(getLifecycleProvider(), new WanObserver<ListBean<List<CollectBean>>>() {
                    @Override
                    public void onWanSuccess(ListBean<List<CollectBean>> listWanBean) {
                        changeState();
                        mOver = listWanBean.getData().getOver();
                        List<CollectBean> list = listWanBean.getData().getDatas();
                        if (mRefresh) {
                            observableList.clear();
                        }
                        mSize = mSize + 1;
                        CollectItemViewModel itemViewModel;
                        for (CollectBean collectBean : list) {
                            itemViewModel = new CollectItemViewModel(CollectViewModel.this, collectBean);
                            observableList.add(itemViewModel);
                        }
                        dismissDialog();
                    }

                    @Override
                    public void onWanFailure(String e) {
                        changeState();
                        dismissDialog();
                        KLog.e(e);
                    }
                }));
    }

    private void changeState() {
        if (mRefresh) {
            uc.finishRefreshing.set(!uc.finishRefreshing.get());
        } else {
            uc.finishLoadmore.set(!uc.finishLoadmore.get());
        }
    }
}
